<template>
  <div id="app">
    <div class="container">
      <div class="close">
        <span class="iconfont iconicon-test"></span>
      </div>
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="inputs">
        <hmInput
          ref="username"
          v-model="user.username"
          placeholder="请输入手机号"
          :reg="/^1[3|4|5|6|7|8|9]\d{9}$/"
        ></hmInput>
        <hmInput
          ref="password"
          placeholder="请输入密码"
          class="input"
          type="password"
          v-model="user.password"
        ></hmInput>
      </div>
      <p class="tips">
        没有账号？
        <a href="#/register" class="">去注册</a>
      </p>
      <van-button icon="star-o" round type="danger" block @click="loginFn"
        >登录按钮</van-button
      >
    </div>
  </div>
</template>

<script>
import hmInput from "@/components/hmInput.vue";
import { login } from "@/api/user.js";

export default {
  components: { hmInput, login },
  data() {
    return {
      user: {
        username: "", // 手机号码
        password: "", // 密码
      },
    };
  },
  methods: {
    setUserName(val) {
      console.log("val", val);
    },
    // 登录函数
    loginFn() {
      console.log(this.user);
      if (!this.$refs.username.isOk) {
        this.$toast.fail("手机号码输入不正确");
        return;
      }
      if (!this.$refs.password.isOk) {
        this.$toast.fail("密码不为空");
        return;
      }
      // 添加加载效果
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0, // 表示不关闭加载弹窗
      });
      login(this.user).then((res) => {
        console.log("请求成功", res);
        if (res.data.message === "用户不存在") {
          this.$toast.fail(res.data.message);
        } else {
          // 保存用户的token和id
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userid", res.data.data.user.id);
          // 登录成功，跳转到首页
          this.$toast.success(res.data.message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: (27 / 360) * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: (126 / 360) * 100vw;
    color: #d81e06;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  text-align: right;
  margin-bottom: 20px;
  a {
    color: #3385ff;
  }
}
</style>>
